import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from 'vuex';

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/pages/home/home.vue')
  },
  {
    path: '/smartEnergy',
    name: 'smartEnergy',
    component: () => import('@/pages/smartEnergy/smartEnergy.vue'),
    children: [
      {
        path: '',
        redirect: 'energyList',
      },
      {
        path: 'energyList',
        name: 'energyList',
        component: () => import('@/pages/smartEnergy/energyList.vue'),
      },
      {
        path: 'energyDetail/:id',
        name: 'energyDetail',
        component: () => import('@/pages/smartEnergy/energyDetail.vue'),
        props: true
      }
    ]
  },
  {
    path: '/digitization',
    name: 'digitization',
    component: () => import('@/pages/digitization/digitization.vue'),
    children: [
      {
        path: '',
        redirect: 'digList',
      },
      {
        path: 'digList',
        name: 'digList',
        component: () => import('@/pages/digitization/digList.vue'),
      },
      {
        path: 'digDetail/:id',
        name: 'digDetail',
        component: () => import('@/pages/digitization/digDetail.vue'),
        props: true
      }
    ]
  },
  {
    path: '/iot',
    name: 'iot',
    component: () => import('@/pages/iot/iot.vue'),
    children: [
      {
        path: '',
        redirect: 'iotList'
      },
      {
        path: 'iotList',
        name: 'iotList',
        component: () => import('@/pages/iot/iotList.vue'),
      },
      {
        path: 'iotDetail/:id',
        name: 'iotDetail',
        component: () => import('@/pages/iot/iotDetail.vue'),
        props: true
      }
    ]
  },
  {
    path: '/service',
    name: 'service',
    component: () => import('@/pages/servicePage/service.vue'),
    children: [
      {
        path: '',
        redirect: 'serList'
      },
      {
        name: 'serList',
        path: 'serList',
        component: () => import('@/pages/servicePage/serList.vue'),
      },
      {
        name: 'serDetail',
        path: 'serDetail/:id',
        component: () => import('@/pages/servicePage/serDetail.vue'),
        props: true
      }
    ]
  },
  {
    path: '/case',
    name: 'case',
    component: () => import('@/pages/case/case.vue'),
    children: [
      {
        path: '',
        redirect: 'caseLists',
      },
      {
        path: 'caseLists',
        name: 'caseLists',
        component: () => import('@/pages/case/CaseLists.vue')
      },
      {
        path: 'caseDetail/:id',
        name: 'caseDetail',
        component: () => import('@/pages/case/CaseDetail.vue')
      }
    ]
  },
  {
    path: '/news',
    name: 'news',
    component: () => import('@/pages/news/news.vue'),
    children: [
      {
        path: '',
        redirect: 'newsLists',
      },
      {
        path: 'newsLists',
        name: 'newsLists',
        component: () => import('@/pages/news/NewsLists.vue')
      },
      {
        path: 'newsDetail/:id',
        name: 'newsDetail',
        component: () => import('@/pages/news/NewsDetail.vue')
      }
    ]
  },
  {
    path: '/company',
    name: 'company',
    component: () => import('@/pages/company/company.vue')
  },
  {
    path: '/contact',
    name: 'contact',
    component: () => import('@/pages/contact/contact.vue')
  },

  {
    path: '/login',
    name: 'login',
    component: () => import('@/pages/loginPage/login.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('@/pages/user/user.vue')
  },
]
var router = new Router({
  // mode: "history",
  routes
})


const blackList = '';

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const token = ''
  if (token) {
    // 用户信息不存在
    if (!store.getters.userInfo) {
      // await store.dispatch('GetUser'); // 获取用户信息
      // router.addRoutes(store.getters.addRoutes);
    } else {
      next();
    }
  } else {
    if (blackList.includes(to.path)) {
      // window.location.href = store.getters.api.IPORTAL_LOCAL_API;
      NProgress.done();
    } else {
      // 不是黑名单，直接进入
      next();
    }
  }
});
router.afterEach(() => {
  NProgress.done()
  router.afterEach((to, from, next) => {
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
  });
})
export default router

